package Servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UserDao;
import model.PersonModel;

/** 
 * Servlet implementation class DeleteServlet
 */
@WebServlet("/DeleteServlet")
public class DeleteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		String content1="";
		String content2="";
		String path="";
		boolean flag=false;
		request.setCharacterEncoding("UTF-8");
		String username = request.getParameter("username");
		
		UserDao userDao = new UserDao();
		
		PersonModel person = new PersonModel();
		
		person.setUsername(username);
		
		int num = userDao.delte(person);
		
		System.out.println("num=="+num);
		if(num==1) {
				
			ArrayList<PersonModel> list = userDao.selectAll();
			request.getSession().setAttribute("list", list);
			
			response.sendRedirect("/Test2022/web/js/showAll.jsp");
			
			
			
		}else {
			flag=true;
			content1="对不起，删除失败";
			content2="返回";
			path="/Test2022/web/js/showAll.jsp";
			
	
			
		}
//		response.setContentType("text/text;charset=utf-8");
        if (flag) {
        	  request.getSession().setAttribute("content1", content1);
  			  request.getSession().setAttribute("content2", content2);
  			 request.getSession().setAttribute("path", path);
  			
  			response.sendRedirect("/Test2022/web/js/PasswordWrong.jsp");
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
